<!--
@Author bhudy
@DescriptSysPluginugin
@Date Tue Nov 05 23:15:21 CST 2019
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>BHUDY - 代码生成</title>
    <link rel="icon" href="/res/images/ico.png" type="image/x-icon">
    <link href="/res/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
    <script src="/res/js/bhudy.js"></script>
    <style>
        input {
            display: block;
            border: 0px;
        }
    </style>
</head>
<body>
<div class="container-fluid">
    <form name="columns" id="columns" class="form-inline">
        <div class="form-group">
            <label for="tableName">表名</label>
            <select class="form-control" id="tableName" name="tableName">
                <option value="0">请选择</option>
            </select>
        </div>

        <div class="form-group">
            <label for="className">包名</label>
            <input class="form-control" id="className" name="className"/>
        </div>

        <div class="checkbox">
            <label for="select"><input class="form-control" id="select" name="select" type="checkbox"
                                       checked/>查询</label>
        </div>

        <div class="checkbox">
            <label for="insert"><input class="form-control" id="insert" name="insert" type="checkbox"
                                       checked/>添加</label>
        </div>

        <div class="checkbox">
            <label for="update"><input class="form-control" id="update" name="update" type="checkbox"
                                       checked/>修改</label>
        </div>

        <div class="checkbox">
            <label for="delete"><input class="form-control" id="delete" name="delete" type="checkbox"
                                       checked/>删除</label>
        </div>

        <button class="btn btn-info" type="button" id="create">提交</button>
        <div class="table-responsive">
            <table class="table table-bordered table-hover" id="io" border="1">
                <tr>
                    <th>field字段名</th>
                    <th>column表列名</th>
                    <th>type字段属性</th>
                    <th>dataType数据属性</th>
                    <th>jdbcTypeJDBC属性</th>
                    <th>key键</th>
                    <th>条件查询</th>
                    <th>comment描述</th>
                </tr>
            </table>
        </div>
    </form>
</div>
</body>
<script>
    // 加载所有表
    $(function () {
        $.getJSON("/api/v1/sysTables", "", function (resultData) {
            var html = "<option value=\"0\">请选择</option>";
            $.each(resultData.data, function (index, val) {
                html += "<option value=\"" + val + "\">" + val + "</option>";
            });
            $("#tableName").html(html);
        });
    });


    // 下拉框值改变时加载所有数据
    $("#tableName").change(function () {
        if ($("#tableName").val() != 0) {
            $.getJSON("/api/v1/sysColumns", "tableName=" + $("#tableName").val(), function (resultData) {
                var html = "<tr><th>field字段名</th><th>column表列名</th><th>type字段属性</th><th>dataType数据属性</th><th>jdbcTypeJDBC属性</th><th>key键</th><th>条件查询</th><th>comment描述</th></tr>";
                $.each(resultData.data, function (index, val) {
                    var str = val.Type.split("(", val.Type.length)[0];
                    /*var type = str == "varchar" ? "String" : str == "int" ? "Integer" : str == "date" ? "Date" : str == "datetime" ? "Date" : str == "text" ? "String" : str;
                    var jdbcType = str == "varchar" ? "VARCHAR" : str == "int" ? "INTEGER" : str == "date" ? "DATE" : str == "datetime" ? "TIMESTAMP" : str == "text" ? "VARCHAR" : str;*/

                    html += "<tr>";
                    html += "<td><input name=\"fields\" value=\"" + val.Field + "\"/></td>";
                    html += "<td><input name=\"columns\" value=\"" + toLowerCase(val.Field) + "\"/></td>";
                    html += "<td><input name=\"types\" value=\"" + getType(str) + "\"/></td>";
                    html += "<td><input name=\"dataTypes\" value=\"" + val.Type + "\"/></td>";
                    html += "<td><input name=\"jdbcTypes\" value=\"" + getJdbcType(str) + "\"/></td>";
                    if (val.Key == "PRI") {
                        html += "<td><input name=\"keys\" placeholder=\"" + val.Key + "\" value=\"@PrimaryKey\"/></td>";
                    } else {
                        html += "<td><input name=\"keys\" placeholder=\"" + val.Key + "\"/></td>";
                    }
                    html += "<td><select name=\"selectList\"><option value=\"\">不选择</option><option value=\"and\">and</option><option value=\"or\">or</option><option value=\"andLike\">andLike</option><option value=\"orLike\">orLike</option></select></td>";
                    html += "<td><input name=\"comments\" value=\"" + val.Comment + "\"/></td>";
                    html += "</tr>";
                })
                $("#io").html(html);

                $("#className").val(toUpperCase($("#tableName").val()));
            });
        }
    });

    function getType(str) {
        if (str.indexOf('varchar') !== -1 || str.indexOf('text') !== -1 || str.indexOf('character') !== -1) {
            return 'String'
        } else if (str.indexOf('bigint') !== -1) {
            return 'Long'
        } else if (str.indexOf('int') !== -1 || str.indexOf('integer') !== -1 || str.indexOf('smallint') !== -1) {
            return 'Integer'
        } else if (str.indexOf('double') !== -1) {
            return 'Double'
        } else if (str.indexOf('date') !== -1 || str.indexOf('datetime') !== -1 || str.indexOf('timestamp') !== -1) {
            return 'Date'
        } else {
            return str
        }
    }

    function getJdbcType(str) {
        if (str.indexOf('varchar') !== -1 || str.indexOf('text') !== -1 || str.indexOf('character') !== -1) {
            return 'VARCHAR'
        } else if (str.indexOf('bigint') !== -1) {
            return 'BIGINT'
        } else if (str.indexOf('int') !== -1 || str.indexOf('integer') !== -1 || str.indexOf('smallint') !== -1) {
            return 'INTEGER'
        } else if (str.indexOf('double') !== -1) {
            return 'DOUBLE'
        } else if (str.indexOf('datetime') !== -1 || str.indexOf('timestamp') !== -1) {
            return 'TIMESTAMP'
        } else if (str.indexOf('date') !== -1) {
            return 'DATE'
        } else {
            return str
        }
    }

    // 字符串格式化，去除下划线，首字母大写，如：user_info：UserInfo
    function toUpperCase(str) {
        var tableNames = str.split("_");
        var s = "";
        for (var i = 0; i < tableNames.length; i++) {
            s += tableNames[i].charAt(0).toUpperCase() + tableNames[i].slice(1);
        }
        return s;
    }

    // 字符串格式化，去除下划线，首字母小写，如：user_info：userInfo
    function toLowerCase(str) {
        var tableNames = str.split("_");
        var s = tableNames[0].charAt(0).toLowerCase() + tableNames[0].slice(1);
        for (var i = 1; i < tableNames.length; i++) {
            s += tableNames[i].charAt(0).toUpperCase() + tableNames[i].slice(1);
        }
        return s;
    }

    // 提交数据
    $("#create").click(function () {
        $.ajaxJsonPOST("/api/v1/sysCreate", $("#columns").serializeJSON(), function (resultData) {
            alert(resultData.code == 0 ? "处理成功" : "处理失败");
        });
    });

</script>
</html>